#ifndef URLFILEDEALER_H_
#define URLFILEDEALER_H_

#include <iostream>
#include <list>
#include <vector>
#include <fstream>

#include "../classes_dados/URLDiskClass.h"


#include "URL.h"
#include <ext/hash_map>
#include <string>
#include <map>
#include <boost/tr1/memory.hpp>
#include <boost/thread/mutex.hpp>


#include "../classes_outros/Config.h"


class URLFileDealer{
private:
    URLFileDealer() 
    :maxNumUrlsFromDisk( Config::getMaxNumUrlsFromDisk() )
    { 	t_hashFile.open("readURLHashes", ios_base::app); }
    
public:
    static std::tr1::shared_ptr<URLFileDealer> getInstance() {return theURLFileDealer;}
    std::list <URL *> getNewURLs();
    void initURLHashSet(/*unsigned long long*/ std::string);
    void addURLs(std::list<URL>);
    void addToDiskBuffer(URL * url);
    void updateDisk0Positions();
    long long faltanteNoDiscoZero(void);
	void writeHashFile(/*unsigned long long*/std::string);
	~URLFileDealer(){ 	t_hashFile.close(); };
private:
	URLDiskClass discoUrl;	
    static std::tr1::shared_ptr<URLFileDealer> theURLFileDealer;
//	__gnu_cxx::hash_map<unsigned long long, bool> urlHashMap;
//    std::map<unsigned long long, bool> urlHashMap;
    std::map<std::string, bool> urlHashMap;
    
	std::vector <URL *> toDiskBuffer;
	
	ofstream t_hashFile;
	
	boost::mutex diskBufferMutex;
	boost::mutex urlHashSetMutex;
	boost::mutex readHashFileMutex;
	 
	const uint maxNumUrlsFromDisk;

//	std::list<URL> listURLtoWrite;
	std::list<URL> listURLtoRead;
};

#endif /*URLFILEDEALER_H_*/
